package views;

import info.clearthought.layout.TableLayout;

import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import models.VarEntry;
import controllers.MainController;

public class VarPanel extends JPanel
{
	/**
	 * Generated serial ID
	 */
	private static final long							serialVersionUID	= -3794344669159440100L;
	
	private MainController								mainController		= null;
	
	private ArrayList<VarEntry>							varList				= null;
	
	private JButton										addVarButton		= null;
	private JButton										removeVarButton		= null;
	
	private int											HGAP				= 5;
	
	public VarPanel(MainController mainController)
	{
		this.mainController = mainController;
		
		setup();
	}
	
	public void addVar()
	{
		VarEntry newVar = new VarEntry(new JLabel("VAR" + varList.size()), new JTextField(3));
		
		mainController.addVar("VAR" + varList.size());
		
		varList.add(newVar);
		
		update();
	}
	
	public void removeVar()
	{
		int toRemove = varList.size() - 1;
		
		if (toRemove >= 0)
			varList.remove(toRemove);
		
		// FIXME: This does not work as it should, workaround while adding new variables
		mainController.removeVar("VAR" + toRemove);
		
		update();
	}
	
	public ArrayList<String> getVars()
	{
		ArrayList<String> result = new ArrayList<String>();
		
		for (VarEntry entry : varList)
		{
			result.add(entry.getValue().getText());
		}
		
		return result;
	}
	
	public void setVars(double step)
	{
		for(VarEntry entry : varList)
		{
			double valToSet = Double.parseDouble(entry.getValue().getText());
			
			entry.getValue().setText(String.valueOf(valToSet + step));
		}
	}
	
	public void update()
	{
		int yPos = 3;
		
		setLayout(new TableLayout(generateSize(varList.size())));
		
		add(getAddVarButton(), "1, 1");
		add(getRemoveVarButton(), "3, 1");
		
		for (VarEntry entry : varList)
		{
			add(entry.getKey(), "1, " + yPos);
			add(entry.getValue(), "3, " + yPos);
			
			yPos += 2;
		}
	}
	
	private double[][] generateSize(int nrOfVars)
	{
		double[] xList = { 5, TableLayout.FILL, 10, TableLayout.FILL, 5 };
		double[] yList = new double[nrOfVars * 2 + 2];
		
		// Initial setup
		yList[0] = 5;
		yList[1] = 25;
		
		for (int i = 2; i < nrOfVars * 2 + 1; i += 2)
		{
			yList[i] = HGAP;
			yList[i + 1] = 20;
		}
		
		double[][] size = { xList, yList };
		
		return size;
	}
	
	public void setup()
	{		
		varList = new ArrayList<VarEntry>();
		
		setLayout(new TableLayout(generateSize(0)));
		
		setBorder(BorderFactory.createTitledBorder("Variable list"));
		
		add(getAddVarButton(), "1, 1");
		add(getRemoveVarButton(), "3, 1");
	}
	
	private JButton getAddVarButton()
	{
		if (addVarButton == null)
		{
			addVarButton = new JButton("Add a variable");
			addVarButton.setActionCommand("ADD_VAR");
			addVarButton.addActionListener(mainController);
		}
		
		return addVarButton;
	}
	
	private JButton getRemoveVarButton()
	{
		if (removeVarButton == null)
		{
			removeVarButton = new JButton("Remove last variable");
			removeVarButton.setActionCommand("REMOVE_VAR");
			removeVarButton.addActionListener(mainController);
		}
		
		return removeVarButton;
	}
}
